package com.egoo.ticket.server.controller;

import com.egoo.ticket.server.config.mq.MqCommonUtil;
import com.egoo.ticket.server.config.mq.RabbitMqConfig;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping(value = "/v1/rabbitmq", produces = "application/json")
@Api(value = "rabbitMq消息管理", tags = "rabbitMq消息管理", description = "rabbitMq消息管理")
public class RabbitMqSendMsgController {

    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private RabbitMqConfig rabbitMqConfig;
    @Autowired
    private MqCommonUtil mqCommonUtil;

	@ApiOperation(value = "work模式发送消息", notes = "work模式发送消息")
	@RequestMapping(method = RequestMethod.GET, value="/sendMsgByWork")
	public DbResponse sendMsgByWork(@ApiParam(value = "消息内容", required = true) @RequestParam("content") String content){
		try {
			mqCommonUtil.pushToMsgTriggerQueue(content);
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,"mq发送成功！");
		} catch (AmqpException e) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"mq发送异常！"+e);
		}
	}
//	@ApiOperation(value = "fanout模式发送消息", notes = "fanout模式发送消息")
//	@RequestMapping(method = RequestMethod.GET, value="/sendMsgByPubSub")
//	public DbResponse sendMsgByPubSub(@ApiParam(value = "消息内容", required = true) @RequestParam("content") String content){
//		amqpTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_FANOUT,"", content);
//		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,"消息发送成功！");
//	}
//	@ApiOperation(value = "topic模式发送消息", notes = "topic模式发送消息")
//	@RequestMapping(method = RequestMethod.GET, value="/sendMsgByTopic")
//	public DbResponse sendMsgByTopic(@ApiParam(value = "消息内容", required = true) @RequestParam("content") String content){
//		for (int i = 0; i < 10; i++) {
//			if (i % 2 == 0) {
//				amqpTemplate.convertSendAndReceive("exchange_topic", "topic.km.topic", "测试发布订阅模型：" + i);
//			} else {
//				amqpTemplate.convertSendAndReceive("exchange_topic", "topic.km", "测试发布订阅模型：" + i);
//
//			}
//		}
//		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,"消息发送成功！");
//	}
}
